package com.lovo.csc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.csc.bean.PromotionalSchemesBean;
import com.lovo.csc.mapper.IPromotionalSchemesMapper;
import com.lovo.csc.service.IPromotionalSchemesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 促销方案service
 */
@Service
@Transactional
public class PromotionalSchemesServiceImpl implements IPromotionalSchemesService {
    @Autowired
    private IPromotionalSchemesMapper promotionalSchemesMapper;

    @Override
    public IPage<PromotionalSchemesBean> findByPage(Integer pageNum) {
        QueryWrapper<PromotionalSchemesBean> qw = new QueryWrapper<>();
        qw.eq("p_status",0);
        return promotionalSchemesMapper.selectPage(new Page<>(pageNum, 5), qw);
    }

    @Override
    public void addPromotional(PromotionalSchemesBean promotionalSchemesBean) {
        //添加促销方案表
        promotionalSchemesMapper.insert(promotionalSchemesBean);
        //添加促销方案表和商品表的中间表
        addPromotionalByProduct(promotionalSchemesBean.getPromotionalId(),
                promotionalSchemesBean.getProductIdArr(),
                promotionalSchemesBean.getRate());
    }

    @Override
    public void addPromotionalByProduct(long promotionalId,Integer[] productIdArr, float rate) {
        promotionalSchemesMapper.addPromotionalByProduct(promotionalId,productIdArr,rate);
    }

    @Override
    public void update(long promotionalId, int audit) {
        QueryWrapper<PromotionalSchemesBean> qw = new QueryWrapper<>();
        qw.eq("pk_promotionalId",promotionalId);
        PromotionalSchemesBean promotionalSchemesBean = promotionalSchemesMapper.selectOne(qw);
        //修改
        promotionalSchemesBean.setAudit(audit);
        promotionalSchemesMapper.updateById(promotionalSchemesBean);
    }

    @Override
    public PromotionalSchemesBean findById(long promotionalId) {
        return promotionalSchemesMapper.selectById(promotionalId);
    }
}
